﻿namespace Sai.Core.Reporting
{
	using System.IO;

	public class XmlTransformerFactory
	{
        public ITextTransformer CreateFromResource(string resourceName)
		{
			string xsltContent = this.ReadResource(resourceName);
			return new XmlTransformer(xsltContent);
		}

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times")]
        private string ReadResource(string resourceName)
		{
			using (Stream resourceStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
			{
                if (resourceStream == null)
                {
                    throw new ReportGenerationException(string.Format("Resource \"{0}\" is not embedded.", resourceName));
                }

				using (StreamReader reader = new StreamReader(resourceStream))
				{
					return reader.ReadToEnd();
				}
			}
		}
	}
}
